import os, sys,shutil,re,time

def fmove(p1,p2):
    if not os.path.exists(p1):return p1+' does not exists '
    if not os.path.exists(p2):return p2+' does not exists '
    shutil.move(p1,p2)
    return False

class logscan:
    def __init__(self):
        self.inname='./in/'
        self.tmpname='./in/temp/'
        self.badname='./in/temp/bad/'
        self.okayinname='./in/temp/okayin/'
        self.sleeptime=5

    def scaner(self):
#        if tmp : print tmp
        tmp=os.listdir(self.inname)
#        print tmp
        for f in tmp:
            pathname = os.path.normpath(os.path.join(self.inname, f))
            if os.path.isfile(pathname) and not os.path.islink(pathname):
                self.logistic(pathname)
                break

    def logistic(self,pathname):
        (dirname,filename)=os.path.split(pathname)
        print 'found = ' ,filename,
        m = fmove(pathname,self.tmpname)
        if m :
            print m
            return
        else:
            print ', moved',
        self.tester(filename)

    def tester(self,filename):
        print ', tst:',
        os.chdir(self.tmpname)
        res = os.popen3('7z t '+filename+ ' ', 't')[1].read()
        res2=re.search('(Everything is Ok)',res)
        if res2:
            print res2.group(),
        else:
            res2=re.search('(Error:)(.*)\n',res)
            if res2:print res2.group(),
            #print res2
        print ', ',

    def run(self):
        while True:
            fff=self.scaner()
            if fff : self.logistic(fff)
            time.sleep(self.sleeptime)

###############################################################################
print '<<<start>>>'
l=logscan()
l.run()